<h1>Report : Sale available products</h1>

<div>
    <form method="post" action="<?php echo $this->createUrl('/Shop/admin/product/exportSaleAvailableProductsReport'); ?>">
        <input class="btn btn-primary" type="submit" name="export-to-excel" value="Export to Excel" />
        <input class="btn btn-primary" type="submit" name="export-to-pdf" value="Export to PDF" />
    </form>
</div>

<?php $grid = $this->widget('Xpress.extensions.web.widgets.XGridView', array(
    'id'=>'sale_available-product-grid',
    'dataProvider'=>$model->searchCustom(true),
    'filter'=>$model,
    'selectableRows'=>2,
    'htmlOptions'=>array('class' => 'table table-hover'),
    'columns'=>array(
        array(
            'name'=>'product.categories',
            'type'=>'raw',
            'filter' => CHtml::activeDropDownList($model, 'category', Yii::app()->controller->getCategoriesForUpdate(), array('prompt' => '---')),
            'value'=>function($data){
                return implode(", ",array_keys($data->categories(array("index"=>"title"))));
            },
            'htmlOptions'=>array('width'=>'15%'),
        ),
        array(
            'name'=>'name',
            'type'=>'raw',
            'value'=>'$data->name',
            'htmlOptions'=>array('width'=>'15%'),
        ),
        array(
            'name' => null,
            'header' => 'SKU',
            'type'=>'raw',
            'filter' => CHtml::activeTextField($model,'id[sku]',array('placeholder'=>'SKU')),
            'value' => function ($data) {
                $subProducts = $data->subProduct;
                if (is_array($subProducts) && count($subProducts)) {
                    foreach ($subProducts as $model) {
                        echo '<p id="sub_product_'.$model->id.'">'.$model->sku.'</p>';
                    }
                }
            },
            'htmlOptions'=>array('width'=>'15%'),
        ),
        array(
            'header' => 'Stock Total',
            'filter' => false,
            'type'=>'raw',
            'value' => function ($data) {
                $subProducts = $data->subProduct;
                if (is_array($subProducts) && count($subProducts)) {
                    foreach ($subProducts as $model) {
                        echo '<p>'.intval($model->stock_total).'</p>';
                    }
                }
            },
            'headerHtmlOptions'=>array('width'=>'15%','style'=>'text-align: center;'),
            'htmlOptions'=>array('width'=>'15%','style'=>'text-align: center;'),
        ),
        array(
            'header' => 'Stock Reserved',
            'filter' => false,
            'type'=>'raw',
            'value' => function ($data) {
                $subProducts = $data->subProduct;
                if (is_array($subProducts) && count($subProducts)) {
                    foreach ($subProducts as $model) {
                        echo '<p>'.intval($model->stock_reserved).'</p>';
                    }
                }
            },
            'headerHtmlOptions'=>array('width'=>'15%','style'=>'text-align: center;'),
            'htmlOptions'=>array('width'=>'15%','style'=>'text-align: center;'),
        ),
        array(
            'header' => 'Quantity On Hand',
            'filter' => false,
            'type'=>'raw',
            'value' => function ($data) {
                $subProducts = $data->subProduct;
                if (is_array($subProducts) && count($subProducts)) {
                    foreach ($subProducts as $model) {
                        echo '<p>'.(intval($model->stock_total) - intval($model->stock_reserved)).'</p>'; 
                    }
                }
            },
            'headerHtmlOptions'=>array('width'=>'15%','style'=>'text-align: center;'),
            'htmlOptions'=>array('width'=>'15%','style'=>'text-align: center;'),
        ),
    ),
)); 
?>